package com.lagou.edu.utils;


import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class ComponentScanUtils {


    private static List<String> classNameList = new ArrayList<>();


    /**
     * 递归获取.class结尾的文件
     * @param packageName
     * @return
     */
    public static List<String> getClassName(String packageName){

        URL resource = ComponentScanUtils.class.getClassLoader().getResource(packageName.replaceAll("\\.", "/"));
        File dir = new File(resource.getFile());
        for(File file : dir.listFiles()){
            if(file.exists()){
                //是文件夹
                if(file.isDirectory()){
                    getClassName(packageName + "." + file.getName());
                }else{
                    if(isClassFile(file.getName())){
                        String className = file.getName().replace(".class","");
                        String fullname = packageName + "." + className;
                        classNameList.add(fullname.trim());
                    }
                }
            }
        }
        return classNameList;
    }

    private static boolean isClassFile(String name) {
        return name.endsWith(".class");
    }

    public static void main(String[] args) {
        List<String> className = getClassName("com.lagou.edu");
        System.out.println(className);
    }


}